 $(document).ready(function(){
 $('#room').hover(function(){
	 $('.hide_menu').show();
 return false;
 },
 function(){
	 $('.hide_menu').hide();	 
return false;
 }
 ); 
 /*
  * xu ly chuyen anh
  * */
 var i=1;
 $('.prev_bt').click(function(){	 
	 for(i=1;i<=9;i++){
		 $('.img_'+i).animate({left:"-=115",opacity:"1",filter:"alpha(opacity=100)"}, 300);
	 }	
 });
 $('.next_bt').click(function(){
	 for(i=1;i<=9;i++){
		 $('.img_'+i).animate({left:"+=115",opacity:"1",filter:"alpha(opacity=100)"}, 300);
	 }			
	 });
 
 
 $(".calendar").hide();
 $('.time_view_').hide();

var room_category;
var kaidan;
var phong;

$('.phong').change(function(){
	phong=$(this).val();
//	alert(phong);
	return false;
	
});

$('.room_category').change(function(){
	room_category=$(this).val();
//	alert(room_category);
	$.ajax({
		url:'viewx.action',
		 type: 'POST',
		 data: 'roomType='+room_category,
		error: function(){  
	          alert('Error');
	      },
		 success: (function(data){  
			 $.each(data,function(index,entry){
				$('.situ_detail').html(room_category);
				$('.price_detail').html(entry.price);
				$('.decription_detail').html(entry.description);
			 });		 
		 })
	});	
	$(".calendar").show();
	 return false;
});

$('.kaidan').change(function(){
	kaidan=$(this).val();
	return false;	 
});



$('.color_td').css({backgroundColor: 'green'});
var dateAsString;// bien nay se lay gia tri cua ngay do
var dateAsObject;


function compareDate(date1,date2)
{
	var arr_date1=date1.split('-');
	var arr_date2=date2.split('-');
	
	var dd1=arr_date1[0];
	var mm1=arr_date1[1];
	var yy1=arr_date1[2];
	d1=parseInt(dd1);
	m1=parseInt(mm1);
	y1=parseInt(yy1);
	
	var dd2=arr_date2[0];
	var mm2=arr_date2[1];
	var yy2=arr_date2[2];
	d2=parseInt(dd2);
	m2=parseInt(mm2);
	y2=parseInt(yy2);
	
	if(y1<y2)
		return -1;
	else if(y1>y2){		
		return 1;
}
	else
		{
			if(m1<m2)
				return -1;
			else if(m1>m2){				
				return 1;
				}
			else				
				{
					if(d1<d2)
						return -1;
					else if(d1>d2)
						{
						return 1;
						}
					else return 0;
				
				}
		}		
	
}


$(".calendar").datepicker({	
	 		 dateFormat: 'd-m-yy',		 		
	 		 onSelect: function(dateText, inst) { 
	 			//$('.color_td').css({backgroundColor: 'green'});
		     dateAsString = dateText; //the first parameter of this function
		     
		     
		     var d = new Date();
		 	var curr_date = d.getDate();
		 	var curr_month = d.getMonth()+1;
		 	var curr_year = d.getFullYear();
		 	var curr_hour=d.getHours();
		 	var curr_min=d.getMinutes();
		 	var now=curr_date+'-'+curr_month+'-'+curr_year; // ngay hien tai
		 	var check=	compareDate(dateAsString, now);

		 	if(check==-1)
		 		{
		 			alert("予約できません。ごめんね！");
		 			$('.time_view_').hide();
		 		
		 		}
		 	else
		 		{
	     
		     $('.tmp_hiden').val(dateAsString);
		     dateAsObject = $(this).datepicker( 'getDate' ); //the getDate method
		 //   alert(dateAsString);
	   $.ajax({
		 		url:'yoyaku.action',
		 		 type: 'POST',
		 		 data: 'room_category='+room_category+'&kaidan='+kaidan+'&date='+dateAsString,
		 		error: function(){  
		 	          alert('Error');
		 	      },
		 		 success: (function(data){  
		 			 // hienthi len khoang time avaible
		 			 $('.time_view_').show();		 		
		 			$('.color_td').css({backgroundColor: 'green'});
		 			var arrayT=new Array();
		 			arrayT=["06:00","06:30","07:00","07:30","08:00","08:30","09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30","18:00"];

		 			$.each(data,function(index,entry){
		 				var index_S;
		 				var index_E;
		 				//$('.input_time').append(entry.startTime);
		 				var startT=entry.startTime;
		 				var endT=entry.endTime;
		 				for(var i=0;i<arrayT.length;i++)
		 					{
		 						if(startT==arrayT[i])
		 							{
		 								index_S=i;
		 							
		 							}
		 						if(endT==arrayT[i])
		 							{
		 								index_E=i;
		 								break;
		 							
		 							}
		 					
		 					}
		 				for(var j=index_S;j<=index_E;j++)
		 					{
		 						$('.b_time_line_'+j).css({backgroundColor: 'red'});
		 					}
		 			
		 			});
		 		 })
		 		
		 		
		 	});
	   
		 		
		
		  return false;
		   }
	 }
});

var roomType1;
var roomType2;
var roomNumber;
var date;
var StartTime;  
var EndTime;


$('.submit_yoyaku').click(function(){// thuc hien viec check time trong input text
	date=$('.tmp_hiden').val();
    StartTime=$('.input_time_box_start').val();
    EndTime=$('.input_time_box_end').val();
    /*--check starttime,endtime co thuoc trong khoang dang xet--*/
if((StartTime=="")||(EndTime=="")||(StartTime==EndTime)||(StartTime>=EndTime))
	{
		alert("キャンセルできません。ごめんね！");		
	}
else
	{

   //alert(date);
	  $.ajax({
	 		url:'orderx.action',
		type: 'POST',
		data: 'floor='+kaidan+'&roomType2='+room_category+'&roomNumber='+phong+'&date='+date+'&StartTime='+StartTime+'&EndTime='+EndTime,
		error: function(){  
	          alert('Error');
	      },
		 success: (function(data){ 
		//	if(data=="ok")
			//	{
					alert("予約するのは完成です。‚");									 
					javascript:location.href ='state.action';
				
		/*		}
			else
				{
					alert("å…¥åŠ›ã�•ã‚Œã�ŸçµŒè·¯ã�¯ã€�æ—¢ã�«ç™»éŒ²ã�•ã‚Œã�¦ã�„ã�¾ã�™ã€‚");					
	 					 javascript:location.href ='yoyaku.jsp';
				}
			*/
	 		 })
	 		    
	   });
	}
});

//check session
$('.session_check').click(function(){
	var ka=$('.session_hd').val();
	alert("lal");
		
		
	
});

$('.cancel_yoyaku').click(function(){
	 if(confirm('キャンセルします。よろしいでしょうか。'))
	 {
		 javascript:location.href ='state.action';
	 }
	 return false;
});


function checkCancel(hour1,hour2,min2)
{
	var hm1=hour1.split(":");
	var h1=hm1[0];
	var m1=hm1[1];

	h1=parseInt(h1);
	m1=parseInt(m1);
	h2=parseInt(hour2);
	m2=parseInt(min2);
	if(h2<h1)
		{		
		hieu=(h2-h1)*60;	
		result=hieu+(m2-m1);
			kq=result/60;
			alert("kq"+kq);
			if(kq<2)
				return 0;
			else return 1;				
		
		}
	return -1;
}

// delete user
$('.delete_room').live('click',function(){
	var roomid=$(this).next('.hidden_room').val();
	 if(confirm('会議室が削除されます。よろしいでしょうか。'))
	 {
		 javascript:location.href ='deleteroom.action?roomID='+roomid+'';
	 }
	 return false;
	
});	
$('.edit_room_').live('click',function(){
	
	var roomid=$(this).prev().prev().prev('.hidden_room').val();
	var ghe=$(this).prev().prev('.hidden_ghe').val();
	var price=$(this).prev('.hidden_use').val();
	 javascript:location.href ='editroom.action?roomID='+roomid+'&ghe='+ghe+'&price='+price+'';	
	
});

//huy yoyaku
$('.cancel_img_bt').live('click',function(){
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth()+1;
	var curr_year = d.getFullYear();
	var curr_hour=d.getHours();
	var curr_min=d.getMinutes();
	var now=curr_date+'-'+curr_month+'-'+curr_year; // ngay hien tai
	
	var hour_min=curr_hour+':'+curr_min; // gio phut hien taj
	
	var tmp_starttime=$(this).parent().prev().prev().prev().prev().prev('.starttime').html();
	//alert(tmp_starttime);
	var tmp_day=$(this).parent().prev().prev().prev('.date').html();
	
	// kiem tra xem ngay trong bang co bang ngay hien tai hay khong
	var check=compareDate(tmp_day, now);
	var check2=checkCancel(tmp_starttime, curr_hour, curr_min);
	if((check==0)&&(check2==0))
		{
			// khi ngay bang nhau thi can so sach starttime de co the cancel duoc
		alert("キャンセルできませんでした。");
		
		}
	else
		{		
			
			if(confirm('キャンセルします。よろしいでしょうか。キャンセル費は5000VNDです。'))
			 {		 
				 var ordercd=$(this).next().val();
				 $.ajax({
				 	url:'delete.action',
					type: 'POST',
					data: 'ordercd='+ordercd,
					error: function(){  
				          alert('Error');
				      },
					 success: (function(data){ 
								alert("キャンセルするのは完成です。");									 
								$('.tr_data').remove();
								
								$('.x_content_right_main').remove();
								$('.x_content_right_main_2').show();
						// dua vao trong bang trong state.jsp
								$.each(data,function(index,entry){
								var content='<tr class="tr_data">';
								content+='<td class="stt">'+index+'</td>';
								content+='<td class="ordercd"> '+entry.orderID+' </td>';
								content+='<td class="roomcd">'+entry.roomID+'</td>';
								content+='	<td class="roomname">'+entry.roomname+'</td>';
								content+='	<td class="starttime">'+entry.startTime+'</td>';
								content+='	<td class="endtime">'+entry.endTime+'</td>';
								content+='	<td class="date">'+entry.day+'</td>';
								content+='	<td class="datcoc">'+entry.deposit+'</td>';
								content+='	<td class="donvi">'+entry.price+'</td>';
								content+='	<td class="tongtien">'+entry.cost+'</td>';
								content+='	<td class="thaotac">';
								content+='	<img name="cancel_img_bt" class="cancel_img_bt" src="image/cancel.gif">';
								content+='	<input type="hidden" class="ordercd_hd" name="ordercd_hd" value='+entry.orderID+'></td></tr>';
								$('.x_content_right_main_tb').append(content);
								
								});						
				 		 })
				 		 	 		    
				   });
				   
			 }
			 return false;
		}
});

$('.listphong').live('change',function(){
	var choise=$(this).val();
	if(choise=="all")
		{
			
			
		}
	else if(choise=="situ")
		{
		
		
		}
	else{
			
			
	}
	
});

// khi cho con tro hover qua nut detail

$('.detai_bt_').hover(
		function(){$('#detail_x').fadeIn(100);
			},
			function(){$('#detail_x').fadeOut(100);}
			);



 });